package test5.lupeng.jf.com.teamdemo.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by C-yx on 2017/7/27.
 */

public class BottomLayout extends LinearLayout {
    private static final String TAG = "BottomLayout";
    public BottomLayout(Context context) {
       this(context,null);
    }

    public BottomLayout(Context context, @Nullable AttributeSet attrs) {
       this(context,attrs,0);
    }

    public BottomLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public  void setResource(ArrayList<TabBean> tabBeen){
        initView(tabBeen);
    }

    private  void initView(final ArrayList<TabBean> tabBeen){
        for (int i = 0; i <4 ; i++) {
            final TabView  tabView = new TabView(getContext(),
                    tabBeen.get(i).getDesStr(),
                    tabBeen.get(i).getResId());
            tabView.getIcon().setColorFilter(Color.GRAY);
            tabView.getTextView().setTextColor(Color.GRAY);
            LinearLayout.LayoutParams  params = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.weight=1;
            tabView.setLayoutParams(params);
            tabView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position =0;
                    for (int j = 0; j <getChildCount(); j++) {
                        getChildAt(j).setSelected(false);
                    }
                    tabView.setSelected(true);
                    for (int j = 0; j < getChildCount(); j++) {
                        Log.e(TAG, "onClick: "+getChildAt(j).isSelected() );
                        if(getChildAt(j).isSelected()){
                            position=j;
                            Log.e(TAG, "onClick: "+j );
                            break;
                        }
                    }

                    if(mFragmentChangeListener!=null){
                        mFragmentChangeListener.setFragment(position);
                    }
                }
            });


            if(i==0){
                tabView.setSelected(true);
            }
            addView(tabView);
        }
    }
    public  FragmentChangeListener mFragmentChangeListener;
    public  interface  FragmentChangeListener{
        void setFragment(int position);
    }
    public  void setFragmentChangeListener(FragmentChangeListener mFragmentChangeListener ){
        this.mFragmentChangeListener  =mFragmentChangeListener;
    }
}
